package org.eclipsecon.swtbot.example;

import static org.eclipse.swtbot.swt.finder.SWTBotAssert.assertTextContains;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swtbot.eclipse.finder.Activator;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.utils.FileUtils;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;

@RunWith(SWTBotJunit4ClassRunner.class)
public class PluginTest2 {
    private static SWTWorkbenchBot bot = new SWTWorkbenchBot();

    @BeforeClass
    public static void beforeClass() throws Exception {
        bot.viewByTitle("Welcome").close();
    }

    @AfterClass
    public static void sleep() {
        bot.sleep(5000); // only for demonstration
    }

    @Test
    public void canCreateANewJavaProject() {
        bot.menu("File").menu("New").menu("Project...").click();
        bot.shell("New Project").activate();
        bot.tree().select("Java Project");
        bot.button("Next >").click();
        bot.textWithLabel("Project name:").setText("MyFirstProject");
        bot.button("Finish").click();
        // FIXME Assert the new project has been created.
    }

    @Test
    public void canCreateANewJavaClass() {
        bot.toolbarDropDownButtonWithTooltip("New Java Class").menuItem("Class").click();
        bot.shell("New Java Class").activate();
        bot.textWithLabel("Source folder:").setText("MyFirstProject/src");
        bot.textWithLabel("Package:").setText("org.eclipsecon.project");
        bot.textWithLabel("Name:").setText("HelloWorld");
        bot.button("Finish").click();
        // FIXME Assert the new class has been created.
    }

    @Test
    public void canTypeInTextInAJavaClass() {
        Bundle bundle = Activator.getDefault().getBundle();
        URL entry = bundle.getEntry("test-files/HelloWorld.java");
        String contents = FileUtils.read(entry);
        SWTBotEditor editor = bot.editorByTitle("HelloWorld.java");
        editor.toTextEditor().setText(contents);
        editor.save();
        assertEquals("Typing text in Java class failed", contents, editor.toTextEditor().getText());
    }
    
    @Test
    public void canExecuteJavaApplication() throws Exception {
        bot.menu("Run").menu("Run").click();
        Thread.sleep(1000);  // FIXME !!!
        Widget consoleViewComposite = bot.viewByTitle("Console").getWidget();
        StyledText swtStyledText = (StyledText) bot.widget(instanceOf(StyledText.class), consoleViewComposite);
        SWTBotStyledText styledText = new SWTBotStyledText(swtStyledText);
        assertTextContains("Hello World", styledText);
    }
    
}